{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 快速入门 UMA\n",
    "\n",
    "UMA (Universal Modular Accelerator Interface) 的目标是创建统一的基础设施，方便地将外部加速器集成到 TVM 中。UMA 提供了文件结构、Python 接口类和用于加速器集成的 API。这些接口可以分为 UMA Partitioner、UMA Lower 和 UMA Codgen。\n",
    "\n",
    "{download}`uma_cli.py`：`python uma_cli.py --add_hardware vanilla_accelerator --tutorial vanilla` 可以创建快速模板。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import set_env\n",
    "import tvm\n",
    "from tvm import relay\n",
    "\n",
    "from tvm.relay.backend.contrib.uma.api import UMAPartitioner\n",
    "from tvm.relay.op.contrib.register import get_pattern_table\n",
    "from tvm.relay.testing import resnet, mlp\n",
    "from tvm.relay.backend.contrib.uma import uma_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "workload = \"mlp\"\n",
    "backend =  \"dnnl\"\n",
    "merge = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "partitioner = UMAPartitioner(backend, merge)\n",
    "pattern_table = get_pattern_table(backend)\n",
    "for entry in pattern_table:\n",
    "    partitioner.add_pattern(*entry)\n",
    "\n",
    "if workload == \"resnet\":\n",
    "    net = resnet.get_net(1, 10)\n",
    "elif workload == \"mlp\":\n",
    "    net = mlp.get_net(1, 10)\n",
    "else:\n",
    "    assert False, f\"don't know how to find workload for {workload}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "mod = tvm.ir.IRModule()\n",
    "mod[\"main\"] = net\n",
    "\n",
    "partitioner.register()\n",
    "partitioned_mod = partitioner.partition(mod)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "34e95b0948f576614c7863cc780d83f61f9551597d4ec05ab5fbb4cfe73deb20"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
